// ======变量定义,要修改======
def projectName = 'template'
// =========================

// 项目路径
def projectDir = './' + projectName

// jenkins上配置的登录账户
def registryCredentialsId = 'haohan_aliyun_dockerhub'
// 镜像仓库
def localRegistryAddress = 'registry.cn-hangzhou.aliyuncs.com'
// 镜像地址
def imageUrl = localRegistryAddress + '/108sq_wei/' + projectName + ':latest'

// git registry的credentialsId
def gitCredentialsId = 'jenkins_git'
// git源码仓库地址
def gitUrl = 'git.108sq.org:8080/scm/git/java/' + projectName
def gitbranch = 'master'

// Dockerfile路径
def dockerfilePath = 'jenkins/pro/Dockerfile'


node {

    stage('Prepare') {
        withCredentials([[$class          : 'UsernamePasswordMultiBinding', credentialsId: gitCredentialsId,
                          usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
            sh 'rm -rf ' + projectName
            sh 'git clone -b ' + gitbranch + ' http://${USERNAME}:${PASSWORD}@' + gitUrl + ' ' + projectName
        }
    }

    stage('mvn build') {
        dir(projectDir) {
            sh 'mvn -version'
            sh 'mvn  -Dmaven.test.skip=true clean package'
            sh 'cp target/*.jar /home/'
        }
    }
    stage('image build') {
        dir(projectDir) {
            withCredentials([[$class          : 'UsernamePasswordMultiBinding', credentialsId: registryCredentialsId,
                              usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
                sh 'sudo docker login -u ${USERNAME} -p ${PASSWORD} ' + localRegistryAddress
                sh 'sudo docker build -t ' + imageUrl + ' -f ' + dockerfilePath + ' .'
                sh 'sudo docker push ' + imageUrl
            }
        }
    }
}
